<?php

namespace App\Http\Controllers\Foundation;


use App\Model\Foundation\TaskReply;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Http\Controllers\MessagePacket;


class TaskReplyController extends BaseController
{
    use MessagePacket;


    /**
     * 提交作业
     * @param Request $request
     * @param $id
     * @return array
     */
    function commit(Request $request, $id)
    {
        try{
            $data = true;
            $obj = TaskReply::find($id);
            $obj->content = $request->get('content');

            $obj->status = 1;
            $obj->replyat = time();

            $obj->save();
            return $this->success($data);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }

    /**
     * 创建
     * @param Request $request
     * @return array
     */
    function correct(Request $request, $id)
    {
        try{
            $reply = TaskReply::find($id);
            # 添加
            $obj = new TaskReply();
            $obj->content = $request->get('content');
            $obj->taskid = $reply->taskid;
            $obj->studentuid = $reply->studentuid;
            $obj->scheduleid = $reply->scheduleid;
            $obj->replyat = time();
            $obj->type = TaskReply::REPLY;
            $obj->save();

            # 更新作业提交状态
            $reply->corrected = 1;
            $reply->save();
            return $this->success(true);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }


    /**
     * @param Request $request
     * @param $id
     * @return array
     */
    function getStudentReply(Request $request)
    {
        try{
            if(!$request->has('taskid')){
                throw  new \Exception('taskid required.');
            }
            if(!$request->has('studentuid')){
                throw  new \Exception('studentuid required.');
            }
            $id = $request->get('taskid');
            $stid = $request->get('studentuid');
            $objs = TaskReply::where('taskid', $id)->where('studentuid', $stid)->get();
            return $this->success($objs);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }

    /**
     * 获取所有学生的作业
     * @param Request $request
     * @param $id
     * @return array
     */
    function getTastReplys(Request $request, $id)
    {
        try{
            $objs = TaskReply::where('taskid', $id)->where('type', TaskReply::TASK)->get();
            foreach ($objs as $obj){
                if(empty($obj->content)){
                    $obj->isReplyed = 1;
                }else{
                    $obj->isReplyed = 0;
                }
                if(empty($obj->corrected)){
                    $obj->isCorrected = 1;
                }else{
                    $obj->isCorrected = 0;
                }
            }
            return $this->success($objs);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }


    /**
     * 获取学生自己的作业列表
     * @param Request $request
     * @param $id
     * @return array
     */
    function getStudentReplyList(Request $request, $id)
    {
        try{
            $objs = DB::table('tasks_replys as r')
                            ->select(DB::raw('r.id, r.taskid, t.title as task, s.name as schedule, s.teacheruid, s.coursetypename, s.begintime, r.status, r.corrected'))
                            ->where('r.studentuid', $id)
                            ->where('r.type', TaskReply::TASK)
                            ->leftjoin('schedules as s', 's.scheduleid', 'r.scheduleid')
                            ->leftjoin('tasks as t', 'r.taskid', 't.id')
                            ->get();
            return $this->success($objs);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }


}
